package servlets.customer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pojo.User;
import services.ServiceUser;

public class ServletSignUp extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        User user = new User();

        user.setFname(request.getParameter("fname"));
        user.setLname(request.getParameter("lname"));
        user.setUsername(request.getParameter("uname"));
        user.setEmail(request.getParameter("email"));
        user.setPassword(request.getParameter("password"));
        String date =request.getParameter("birthdate");
        
        
        int year,month,day;
        day= Integer.parseInt(date.split("-")[0]);
        month= Integer.parseInt(date.split("-")[1]);
        year= Integer.parseInt(date.split("-")[2]);
        
        String n = new String(day+"-"+month+"-"+year);
        System.out.println("dataaaaaaaa*(&*(D&AS**SADaaaaaaaaaaaaaaa"+n);
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            java.util.Date newdate =  df.parse(n);
            user.setBirthdate(newdate);
        } catch (ParseException ex) {
            Logger.getLogger(ServletSignUp.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        //Calendar cal = null;
        //cal.getInstance();
        //cal.set(year, month, day);
        //user.setBirthdate(new java.sql.Date(cal.getTimeInMillis()));
        
        
        
        user.setAddress(request.getParameter("address"));
        user.setMobile(request.getParameter("mobile"));
        user.setCredit(request.getParameter("balance"));
        user.setLoginDate(new Date(System.currentTimeMillis()));
        user.setActive(1);
        ServiceUser serviceUser = new ServiceUser();
        response.getWriter().print(serviceUser.register(user));
        ;

    }

}
